<?php
class DealCouponsController extends AppController
{
    var $name = 'DealCoupons';
	function index() 
    {
        $this->pageTitle = __l('Deal Coupons');
        $this->DealCoupon->recursive = 0;
        if ($this->Auth->user('group_id') == ConstUserTypes::Admin) {
			$conditions = array(
				'Deal.id' => $this->params['named']['deal_id']
			);		
		}else{
			$company = $this->DealCoupon->Deal->Company->find('first', array(
				'conditions' => array(
					'Company.user_id' => $this->Auth->user('id')
				) ,
				'recursive' => -1
			));
			if(empty($company)){
				$this->cakeError('error404');
			}
			$conditions = array(
				'Deal.id' => $this->params['named']['deal_id'],
				'Deal.company_id' => $company['Company']['id']				
			);
		}
		$deal = $this->DealCoupon->Deal->find('first', array(
			'conditions' => $conditions,
			'fields' => array(
				'Deal.id',
				'Deal.name',
			),
			'recursive' => -1
		));
		if(empty($deal)){
            $this->cakeError('error404');
		}
		$this->paginate = array(
			'conditions' => array(
				'DealCoupon.deal_id' => $this->params['named']['deal_id']
			),
			'recursive' => -1
		);
		$this->set('deal', $deal);
        $this->set('dealCoupons', $this->paginate());
    }
	function delete($id = null) 
    {
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        if ($this->DealCoupon->del($id)) {
            $this->Session->setFlash(__l('Unused coupon deleted') , 'default', null, 'success');
            $this->redirect(array(
				'controller' => 'deals',
                'action' => 'index'
            ));
        } else {
            $this->cakeError('error404');
        }
    }
	function admin_index()
    {
		$this->setAction('index');
    }	
	function admin_delete($id = null) 
    {
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        if ($this->DealCoupon->del($id)) {
            $this->Session->setFlash(__l('Unused coupon deleted') , 'default', null, 'success');
            $this->redirect(array(
				'controller' => 'deals',
                'action' => 'index'
            ));
        } else {
            $this->cakeError('error404');
        }
    }
}
?>